from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.school.processor.base_processor import BaseProcessor
from com.arcfox.util.muilty_coroutine_util import concurrency
from com.arcfox.util.redis_key_manager import YZY_BASIC_UNIVERSITY_SCHOOL_LIST_KEY


class YZYSchoolListProcessor(BaseProcessor):
    def __init__(self):
        self.basic_task_manager = RedisTaskManager(YZY_BASIC_UNIVERSITY_SCHOOL_LIST_KEY)

    async def parse_and_save_data(self, result_json):
        items = result_json['result']['items']
        data_list = []
        for item in items:
            yzy_code = item['code']
            university_name = item['cnName']
            university_code = item['gbCode']
            data_list.append({
                "yzy_code": yzy_code,
                "university_name": university_name,
                "university_code": university_code,
                "university_uid": self.generate_school_uid(university_name)
            })
        await self.basic_task_manager.add_basic_tasks(data_list)

    @concurrency(5)
    async def __update_university_code(self, **params):
        task = params['task']
        mapping = db.MysqlMapping("tb_university_info")
        await mapping.update({"university_code": task['university_code']}, {"university_uid": task['university_uid']})
